package com.yinhe.module.gs.thought.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.DateUtils;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.framework.config.ServerConfig;
import com.yinhe.system.domain.YhContent;
import com.yinhe.system.domain.YhFileVerify;
import com.yinhe.system.service.IYhContentService;
import com.yinhe.system.service.IYhFileVerifyService;
import com.yinhe.web.controller.tool.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static java.lang.Long.valueOf;

/**
 * 内容公告发布Controller
 * 
 * @author yinhe
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/gs/thought")
public class YhThoughtController extends BaseController
{
    @Autowired
    private IYhContentService yhContentService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IYhFileVerifyService yhFileVerifyService;
    /**
     * 查询内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContent yhContent)
    {
        startPage();


        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        return getDataTable(list);
    }






    /**
     * 查询文件树列表
     * @return
     */
   /* @PreAuthorize("@ss.hasPermi('gs:thought:list')")
    @GetMapping("/listtree")
    public TableDataInfo listtree(YhContent yhContent)
    {

        HttpGet httpPost = new HttpGet("http://localhost:8080/file/getfiletree");
//      随机设计
        String boundary ="--------------4585696313564699";
        //设置请求头(百度好多都没有设置这个请求头)
        httpPost.setHeader("Content-Type","multipart/form-data;boundary="+boundary);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;
        //模拟浏览器
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        //字符编码
        builder.setCharset(Charset.forName("UTF-8"));
        builder.setBoundary(boundary);




//        file 文件这样写
//        builder.addPart("参数名",new FileBody(file));

        HttpResponse resp = null;
        try {

            // 提交
            resp = client.execute(httpPost);
            //根据业务需求 自行调整
            HttpEntity entity = resp.getEntity();
            respContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
        }


        System.out.println(respContent+"respContent");


        return getDataTable(Collections.singletonList(respContent));
    }*/







    /**
     * 导出内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:export')")
    @Log(title = "内容公告发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContent yhContent)
    {
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        ExcelUtil<YhContent> util = new ExcelUtil<YhContent>(YhContent.class);
        //util.hideColumn("noticeTitle","note1");
        //util.hideColumn("note1");
        util.hideColumn("noticeType","pictureUrl","sourcesUrl","participants","viewsTimes","haveExperience",
               "experienceContent","fileNum","userId","deptId","noticeId","documentNo",
                "implementationTime","security","yearsLimit","note2","note1",
                "note3","note4","note5","note6","note7","note8","note9","note10"
        );


        util.exportExcel(response, list, "思政教育发布数据");
    }
    /**
     * 拍照
     */

    @GetMapping("/photo")
    public void photo( @RequestParam("filePatha") String filePatha, @RequestParam("fileName") String fileName)
    {
        System.out.println(filePatha+"filePatha");




            // 上传文件路径
            String filePath = filePatha;


            // 上传并返回新文件名称


            // 获取当前登录用户
            LoginUser loginUser = getLoginUser();
            SysUser sysUser = loginUser.getUser();




            YhFileVerify yhFileVerify=new YhFileVerify();
            yhFileVerify.setDeptId(String.valueOf(sysUser.getDeptId()));
            yhFileVerify.setUserId(String.valueOf(sysUser.getUserId()));
            yhFileVerify.setFilepath("/");
            yhFileVerify.setFilename(fileName);
            yhFileVerify.setChunkNumber(String.valueOf(1));
            yhFileVerify.setRelativePath(fileName);
            yhFileVerify.setTotalChunks(Long.valueOf(1));
            yhFileVerify.setCurrentChunkSize(Long.valueOf(3000));
            yhFileVerify.setTotalSize(Long.valueOf(3000));
            yhFileVerify.setChunkSize(Long.valueOf(3000));
            yhFileVerify.setFileVerifyStatus(String.valueOf(0));
            yhFileVerify.setIdentifier(MD5Utils.generateMD5(fileName));
            yhFileVerify.setUsername(loginUser.getUser().getNickName());
            yhFileVerify.setNote1(filePath);
            if(fileName.contains("pdf")){
                yhFileVerify.setNote2(fileName);
            }else{
                yhFileVerify.setNote3(fileName);
            }

            Integer i = yhFileVerifyService.insertYhFileVerify(yhFileVerify);
            System.out.println(i);






    }

    /**
     * 获取内容公告发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        startPage();


        return AjaxResult.success(yhContentService.selectYhContentById(id));
    }

    /**
     * 新增内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:add')")
    @Log(title = "内容公告发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContent yhContent)
    {// 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhContent.setUserId(sysUser.getUserId());
        yhContent.setDeptId(sysUser.getDeptId());

        yhContent.setReleaseTime(DateUtils.getNowDate());



        return toAjax(yhContentService.insertYhContent(yhContent));
    }

    /**
     * 修改内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:edit')")
    @Log(title = "内容公告发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContent yhContent)
    {
        return toAjax(yhContentService.updateYhContent(yhContent));
    }

    /**
     * 删除内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('gs:thought:remove')")
    @Log(title = "内容公告发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContentService.deleteYhContentByIds(ids));
    }


  /*  *//**
     * tiao获取内容公告发布详细信息
     *//*
    @PreAuthorize("@ss.hasPermi('gs:thought:tiao')")
    @GetMapping(value = "/tiao/{id}")
    private  void gettiao(@PathVariable("id") Long id)
    {
        System.out.println(id+"id");
        //return AjaxResult.success(yhContentService.selectYhContentById(id));
      //  String idx = request.getParameter("id");

    }

    *//**
     * 查询内容公告发布列表
     * 查询内容公告发布列表
     *//*
    @PreAuthorize("@ss.hasPermi('gs:thought:list')")
    @GetMapping("/listX")
    public TableDataInfo listX(YhContent yhContent )
    {
        startPage();


        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        return getDataTable(list);
    }

*/

}
